//
//  UIStatusWindow.m
//  Assistant
//
//  Created by Hunk on 12-3-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIStatusWindow.h"
#import <QuartzCore/QuartzCore.h>

#define ANIMATEDURATION (0.5f)

static UIStatusWindow *_sharedStatusWindow = nil;

@implementation UIStatusWindow

+ (UIStatusWindow *)sharedStatusWindow
{
    @synchronized([UIStatusWindow class])
    {
        if(!_sharedStatusWindow)
        {
            _sharedStatusWindow = [[UIStatusWindow alloc] initWithFrame:CGRectZero];
        }
        return _sharedStatusWindow;
    }
    return nil;
}

- (id)initWithFrame:(CGRect)frame
{
    if((self = [super initWithFrame:frame]))
    {
        // Init property
        self.frame = [UIApplication sharedApplication].statusBarFrame;
        self.backgroundColor = [UIColor clearColor];
        self.windowLevel = UIWindowLevelStatusBar - 1.0f;
        self.alpha  = 1.0;
        self.hidden = NO;
        
//        // Background view
//        _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
//        [_backgroundView setBackgroundColor:TINT_COLOR];
//        _backgroundView.alpha = 0.0;
//        [self addSubview:_backgroundView];
        
        // Background label
        _backgroundLabel = [[UILabel alloc] initWithFrame:self.bounds];
        [_backgroundLabel setBackgroundColor:TINT_COLOR];
        [_backgroundLabel setTextColor:[UIColor whiteColor]];
        [_backgroundLabel setTextAlignment:UITextAlignmentCenter];
        [_backgroundLabel setFont:[UIFont systemFontOfSize:12.0]];
        [_backgroundLabel setAlpha:0.0];
        [self addSubview:_backgroundLabel];
    }
    return self;
}

#pragma mark -
#pragma mark Show status bar
- (void)show:(NSString *)message
{
    self.hidden = NO;
    
    [[UIApplication sharedApplication] setStatusBarHidden:!self.hidden withAnimation:UIStatusBarAnimationSlide];
    
    [UIView animateWithDuration:ANIMATEDURATION 
                     animations:^{
                         [_backgroundLabel setText:message];
                         [_backgroundLabel setAlpha:1.0];
                     }
                     completion:nil];
}

#pragma mark -
#pragma mark Hide status bar
- (void)hide
{
    [UIView animateWithDuration:ANIMATEDURATION
                     animations:^{
                         [_backgroundLabel setAlpha:0.0];
                     } 
                     completion:^(BOOL finished){
                         if(finished)
                         {
                             self.hidden = YES;
                         }
                     }];
    
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}

- (void)dealloc
{   
    [_backgroundView release];
    [_backgroundLabel release];
    
    [super dealloc];
}

@end
